
import 'ByteBuf.dart';
import 'FieldTypeInterface.dart';

class BufferUtil {

  static encode2<T extends FieldTypeInterface>(ByteBuf byteBuf, T t) {
    var fieldMap = t.getFieldTypeMap();
    var fields = fieldMap.keys.toList();
    fields.sort();
    for (var field in fields) {
      FieldWrapper<T, dynamic> fieldWrapper = fieldMap[field] as FieldWrapper<T, dynamic>;
      var fieldType = fieldWrapper!.fieldType;
      if (fieldType == FieldType.string) {
        var value = fieldWrapper!.getter(t) as String?;
        byteBuf.writeNullString(value);
      } else if (fieldType == FieldType.byte) {
        var value = fieldWrapper!.getter(t) as int?;
        byteBuf.writeNullByte(value);
      } else if (fieldType == FieldType.short) {
        var value = fieldWrapper!.getter(t) as int?;
        byteBuf.writeNullShort(value);
      } else if (fieldType == FieldType.int) {
        var value = fieldWrapper!.getter(t) as int?;
        byteBuf.writeNullInt(value);
      } else if (fieldType == FieldType.long) {
        var value = fieldWrapper!.getter(t) as int?;
        byteBuf.writeNullLong(value);
      } else if (fieldType == FieldType.date) {
        var value = fieldWrapper!.getter(t) as DateTime?;
        byteBuf.writeNullDate(value);
      } else {
        throw Exception("不支持这种类型${fieldWrapper.fieldType}");
      }
    }
  }

  static ByteBuf encode<T extends FieldTypeInterface>(T t) {
    var byteBuf = new ByteBuf(1024);
    BufferUtil.encode2(byteBuf, t);
    return byteBuf;
  }

  static T decode<T extends FieldTypeInterface>(T data, ByteBuf byteBuf) {
    var fieldMap = data.getFieldTypeMap();
    var fields = fieldMap.keys.toList();
    fields.sort();

    for (var field in fields) {
      FieldWrapper<T, dynamic> fieldWrapper = fieldMap[field] as FieldWrapper<T, dynamic>;
      var fieldType = fieldWrapper!.fieldType;
      if (fieldType == FieldType.string) {
        var value = byteBuf.readNullString();
        FieldWrapper<T, String?> fieldWrapper2 = fieldMap[field] as FieldWrapper<T, String?>;
        fieldWrapper2.setter(data, value);
      } else if (fieldType == FieldType.byte) {
        var value = byteBuf.readNullByte();
        FieldWrapper<T, int?> fieldWrapper2 = fieldMap[field] as FieldWrapper<T, int?>;
        fieldWrapper2.setter(data, value);
      } else if (fieldType == FieldType.short) {
        var value = byteBuf.readNullShort();
        FieldWrapper<T, int?> fieldWrapper2 = fieldMap[field] as FieldWrapper<T, int?>;
        fieldWrapper2.setter(data, value);
      } else if (fieldType == FieldType.int) {
        var value = byteBuf.readNullInt();
        FieldWrapper<T, int?> fieldWrapper2 = fieldMap[field] as FieldWrapper<T, int?>;
        fieldWrapper2.setter(data, value);
      } else if (fieldType == FieldType.long) {
        var value = byteBuf.readNullLong();
        FieldWrapper<T, int?> fieldWrapper2 = fieldMap[field] as FieldWrapper<T, int?>;
        fieldWrapper2.setter(data, value);
      } else if (fieldType == FieldType.date) {
        var value = byteBuf.readNullDate();
        FieldWrapper<T, DateTime?> fieldWrapper2 = fieldMap[field] as FieldWrapper<T, DateTime?>;
        fieldWrapper2.setter(data, value);
      } else {
        throw Exception("不支持这种类型2${fieldType}");
      }
    }
    return data;
  }
}